/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.bo;

import com.comut.crud.dao.FleetDao;
import com.comut.crud.ejb.interfaces.IFleetAction;
import com.comut.crud.pojo.fleet.Fleet;
import com.comut.crud.util.ParseJson;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import com.comut.lib.exception.JSONMalFormedException;
import com.comut.lib.exception.ObjectFoundException;
import com.comut.lib.exception.ObjectNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class FleetHandler  implements IFleetAction {

    private static Logger _logger = Logger.getLogger(FleetHandler.class.getName());
    private Connection _moovefleetConnection;
    private Connection _ifleetConnection;
    private FleetDao fleetDao;

    public FleetHandler(Connection moovefleetConnection, Connection ifleetConnection) {
        this._moovefleetConnection = moovefleetConnection;
        this._ifleetConnection = ifleetConnection;
        fleetDao = new FleetDao(_moovefleetConnection, _ifleetConnection);
    }

    public Fleet insertFleet(Fleet fleet) throws ObjectFoundException {
        if(fleet.isValid()){
            fleetDao.checkUniqueAlias(-1, fleet.getAlias());
            int id = fleetDao.createFleet(fleet);
            Fleet toRet = new Fleet();
            toRet.setId(id);
            _logger.info(toRet.toString());
            return toRet;
        }
        else
            throw new JSONMalFormedException("The mandatory attributes are not in json.",ErrorCodes.JSON_MAL_FORMED);
    }

    public void deleteFleet(Fleet fleet) throws ObjectNotFoundException {
        _logger.info(fleet.toString());
        if(fleet.getId()>0){
            fleetDao.checkAssociateWithVehicles(fleet.getId());
            fleetDao.deleteFleet(fleet.getId());
        }
        else
            throw new BaseException("Invalid ID", ErrorCodes.INVALID_ID);
    }

    public void updateFleet(String fleet) throws ObjectNotFoundException {
        _logger.info(fleet);
        //check if the Object is well formed
        Fleet fas = ParseJson.getInstance().getFleetUpdateValidated(fleet);
        if(fas.getId()>0){
            fleetDao.checkUniqueAlias(fas.getId(), fas.getAlias());
            //update the fleet
            fleetDao.updateFleet(fas);
        }
        else{
            throw new BaseException("Invalid ID", ErrorCodes.INVALID_ID);
        }
    }

    public Fleet getFleet(Fleet fleet) throws ObjectNotFoundException {
        
        ResultSet rs = fleetDao.getFleet(fleet.getId());
        Fleet toRet = new Fleet();
        try {
            if (rs.next()) {
                toRet.setId(fleet.getId());
                toRet.setAlias(rs.getString("alias"));
            }
        } catch (SQLException ex) {
            _logger.error(ex);
        }
        return toRet;
    }
    
    
}
